// saveUserInfo 云函数（Node版） 
exports.main = async (context) => {  
  try {  
    const cloud = context.cloud;
    const collectionName = context.data.collectionName;
    //保存用户信息  
    const result = await context.cloud.db.collection(collectionName) .insertOne({
      // context 自带的参数  
      user_nick : context.userNick,  
      open_id : context.openId,  
      // 端上传过来的参数   
      num : context.data.num,  
      desc : context.data.desc  
    }).then(res => { 
      console.log('保存数据成功'); 
      console.log(res);
    });  
    return {  
      success: true,  
      result: result 
    };  
  } catch (e) {  
    console.log('fail:',e);  
    return {  
      success: false  
    }  
  } 
};

//会员
exports.getMember = async (context) => {
	let {cloud,mixNick} = context;
	try {
		//参数验证
        const result = await cloud.topApi.invoke({
            api: 'taobao.crm.member.identity.get',
            autoSession: true,
            data: {
                'mix_nick': mixNick,
            }
        });
        
        return {
            status: "success",
            data: result
        }
	} catch (e) {
		return {
            status: "fail",
            data: err
        }
	}
};

//获取入会链接
exports.getMemberUrl = async (context) => {
	const { data: { url }, cloud } = context;

	try {
        const result = await cloud.topApi.invoke({
            api: 'taobao.crm.member.joinurl.get',
            autoSession: true,
            data: {
                'callback_url': url,
                'extra_info': '{\"source\":\"isvapp\",\"activityId\":\"12345677\",\"entrance\":\"hudong\"}',
            }
        });

        return {
            status: "success",
            data: result
        }
	} catch (e) {
		return {
            status: "fail",
            data: err
        }
	}
};
